//
//  PatViewController.m
//  随拍
//
//  Created by lanou3g on 16/1/18.
//  Copyright © 2016年 Ashoka. All rights reserved.
//

#import "PatViewController.h"
#import "PatCollectionViewCell.h"
#import "PatForFlowLayout.h"
#import "VideoForFlowLayout.h"
#import "VideoForCollectionViewCell.h"
#import "PatForModel.h"
#import "UIImageView+WebCache.h"
#import "PatManger.h"
#import "PlayViewController.h"




@interface PatViewController ()<UICollectionViewDelegateFlowLayout,UICollectionViewDataSource>

@property(nonatomic,strong)UICollectionView *VideoForCollectionView;

@property(nonatomic,strong)UICollectionView *PatForCollectionView;

@property(nonatomic,strong)UIScrollView *mainScrollView;



@end

@implementation PatViewController
NSString *identf = @"cells";

NSString *idfi = @"cellss";
- (void)viewDidLoad {
    
    [PatManger sharedPatManger].updateUI = ^(){
        
        [self.VideoForCollectionView reloadData];
        
        [self.PatForCollectionView reloadData];
        
    };
    [self scrollView];

    [self collectionViewFirst];
    
    [self CollectionViewSecond];
    
    [super viewDidLoad];
}


-(void)scrollView{
    
    self.mainScrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    self.mainScrollView.contentSize = CGSizeMake(414, self.view.frame.size.height *5);
    
    self.mainScrollView.showsVerticalScrollIndicator = NO;
    self.mainScrollView.bounces = NO;
    
    [self.view addSubview:self.mainScrollView];
}



-(void)collectionViewFirst{
    PatForFlowLayout *FlowLayout = [[PatForFlowLayout alloc] init];
    
    _PatForCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 340) collectionViewLayout:FlowLayout];
    
    _PatForCollectionView.delegate = self;
    _PatForCollectionView.dataSource = self;
    

    
    [self.mainScrollView addSubview:_PatForCollectionView];
    
    [_PatForCollectionView registerClass:[PatCollectionViewCell class] forCellWithReuseIdentifier:identf];
}

-(void)CollectionViewSecond{
    
    VideoForFlowLayout *Flowlayout = [[VideoForFlowLayout alloc] init];
    
    self.VideoForCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 344, self.view.frame.size.width, self.mainScrollView.frame.size.height *4 ) collectionViewLayout:Flowlayout];
    self.VideoForCollectionView.backgroundColor = [UIColor whiteColor];
    
    self.VideoForCollectionView.delegate = self;
    self.VideoForCollectionView.dataSource = self;
    self.VideoForCollectionView.scrollEnabled = NO;
    [self.mainScrollView addSubview:self.VideoForCollectionView];
    
    [self.VideoForCollectionView registerClass:[VideoForCollectionViewCell class] forCellWithReuseIdentifier:idfi];
    
    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    
    if (collectionView == _PatForCollectionView) {
        

        return [PatManger sharedPatManger].arrays.count;
        
    }else if (collectionView == _VideoForCollectionView){
       
        return [PatManger sharedPatManger].dataArray.count;
    }else{
        return 0;
    }
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    if (collectionView == _PatForCollectionView) {
        PatCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identf forIndexPath:indexPath];
        
        PatForModel *patM = [PatManger sharedPatManger].arrays[indexPath.row];
        
        
   
        [cell.imgView sd_setImageWithURL:[NSURL URLWithString:patM.avatar] placeholderImage:[UIImage imageNamed:@"1.jpg"]];
        
        PatForModel *patforModel = [PatManger sharedPatManger].dataArray[indexPath.row];
        

        cell.caption.text = patforModel.caption;
        
        return cell;
        
    }
    else if(collectionView == _VideoForCollectionView) {
        VideoForCollectionViewCell *cell1 = [collectionView dequeueReusableCellWithReuseIdentifier:idfi forIndexPath:indexPath];
        
       PatForModel *pat = [PatManger sharedPatManger].dataArray[indexPath.row];
        
     
        
    
        [cell1.imgView sd_setImageWithURL:[NSURL URLWithString:pat.cover_pic] placeholderImage:[UIImage imageNamed:@"1.jpg"]];
        
        PatForModel *patModel = [PatManger sharedPatManger].arrays[indexPath.row];
        
      
        
        [cell1.headView sd_setImageWithURL:[NSURL URLWithString:patModel.avatar] placeholderImage:[UIImage imageNamed:@"1.jpg"]];
        

        
        cell1.nameLabel.text = patModel.screen_name;
        
        return cell1;
    }else{
        return  nil;
    }
    
    
    
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (collectionView == _VideoForCollectionView) {
        PlayViewController *play = [[PlayViewController alloc] init];
        
        PatForModel *pat = [PatManger sharedPatManger].dataArray[indexPath.row];
        
        
        play.strurl =pat.video;
        
        
       
        
        
        [self.navigationController pushViewController:play animated:YES];
    }
  
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
